package com.sv.web;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class ViewExcel extends AbstractExcelView {

	/**
	 * 导出方法 :请求使用window.open(url)
	 * 
	 */
//	@RequestMapping(value = "/export")
//	public ModelAndView export(@RequestParam(required = false) String cond) {
//
//		Map<String, Object> paramMap = new HashMap<String, Object>();
//
//		if (StringUtils.isNotBlank(cond)) {
//			// 如果是纯数字则查询商户编号
//			if (RegexpHepler.isHardRegexpValidate(cond,
//					RegexpHepler.POSITIVE_INTEGER_REGEXP)) {
//				paramMap.put("merchantNo", cond);
//			} else {
//				paramMap.put("merchantName",
//						StringUtil.replaceSpecialLikeCharacter(cond));
//			}
//		}
//		IPage<MerchantJoinApply> result = merchantService.findMerchantPageList(
//				paramMap, 1,1000);
//
//		ArrayList<String[]> excel = new ArrayList<String[]>();
//		Map _re = new HashMap();
//		_re.put("orgCode", "sheet");
//		String[] cellsTitle = new String[6];
//		cellsTitle[0] = "商户编号";
//		cellsTitle[1] = "商户名称（简称）";
//		cellsTitle[2] = "联系人";
//		cellsTitle[3] = "联系电话";
//		cellsTitle[4] = "商户类型";
//		cellsTitle[5] = "开通时间";
//		_re.put("cellsTitle", cellsTitle);
//
//		List<MerchantJoinApply> rqr = (List<MerchantJoinApply>) result
//				.getData();
//		for (MerchantJoinApply merchant : rqr) {
//			String[] cells = new String[6];
//			cells[0] = merchant.getMerchantNo();
//			cells[1] = merchant.getMerchantShortName();
//			cells[2] = merchant.getBusinessName();
//			cells[3] = merchant.getContactMobile();
//			cells[4] = merchantTypes.get(String.valueOf(merchant
//					.getMerchantType()));
//			cells[5] = merchant.getAuditDateStr();
//			excel.add(cells);
//		}
//		_re.put("data", excel);
//		_re.put("fileName", "商户信息");
//		return new ModelAndView(new ViewExcel(), _re);
//	}
	
	
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//sheet存在导出文件的机构代码，导入时需要使用此数据项
		String orgCode = (String)model.get("orgCode");
		HSSFSheet sheet = workbook.createSheet(orgCode);
		
		sheet.setDefaultColumnWidth((short) 12);
		String[] cellsTitle = (String[]) model.get("cellsTitle");
		HSSFCell cell = null;
		int i = 0;
		for (String a : cellsTitle) {
			cell = getCell(sheet, 0, i);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			setText(cell, a);
			i = i + 1;
		}

		ArrayList<String[]> excel = (ArrayList<String[]>) model.get("data");
		if(excel!=null){
		
		// 记录行
		int j = 1;
		for (String[] a : excel) {
			i = 0;
			for (String _a : a) {
				cell = getCell(sheet, j, i);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				setText(cell, _a);
				i = i + 1;
			}
			j = j + 1;
		}
		}
		String filename = model.get("fileName") + ".xls";// 设置下载时客户端Excel的名称
		filename = ExcelNameEncode.encodeFilename(filename, request);// 处理中文文件名
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		// response.setContentType("application/msexcel;charset=GB2312");
		response.setHeader("Content-disposition", "attachment;filename="
				+ filename);
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();

	}

}
